//
//  KBoxItem.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/14.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

struct KBoxData {
    var title:String!
    var image:NSImage!
    var imageSelect:NSImage!
}
class KBoxItem: MSView {
    var data:KBoxData?
    
    var isSelected:Bool = false {
        didSet {
            let bgColor = isSelected ? color_main140:color_dark60
            self.layer?.backgroundColor = bgColor.cgColor
            let textColor = isSelected ? color_gray(255):color_dark160
            self.sTitle.textColor = textColor
            if let KData = self.data {
                let img = isSelected ? KData.imageSelect:KData.image
                self.icon.image = img
            }
            
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    convenience init(data:KBoxData) {
        self.init()
        self.data = data
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        guard let kData = data else {
            return
        }
        
        self.sTitle.stringValue = kData.title
        self.icon.image = kData.image
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        self.wantsLayer = true
        self.layer?.backgroundColor = color_dark60.cgColor
        self.layer?.masksToBounds = true
        self.layer?.cornerRadius = 5
        self.layer?.borderWidth = 1
        self.layer?.borderColor = color_dark40.cgColor
        
        self.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.width.equalTo(40)
            make.height.equalTo(40)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-15)
        }
        
        self.addSubview(sTitle)
        sTitle.snp.makeConstraints { (make) in
            make.centerX.width.equalToSuperview()
            make.bottom.equalTo(-20)
            make.height.equalTo(18)
        }
    }
    
    
    lazy var icon: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"icon_clive")!)
        return imageV
    }()
    
    lazy var sTitle: NSLabel = {
        let lab = NSLabel.label(title: "标题", textColor: color_dark160, font: NSFont.systemFont(ofSize: 14))
        lab.sizeToFit()
        lab.alignment = .center
        return lab
    }()
}
